package com.example.hello.client.NavbarButton;

import com.example.hello.BootstrapNavbarButton;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.github.gwtbootstrap.client.ui.resources.ButtonSize;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapNavbarButton.class)
public class BootstrapNavbarButtonConnector extends AbstractComponentConnector implements ClickHandler {

    private BootstrapNavbarButtonRpc rpc = RpcProxy.create(BootstrapNavbarButtonRpc.class, this);

    public BootstrapNavbarButtonConnector() {
        getWidget().addClickHandler(this);
    }

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapNavbarButton.class);
    }

    @Override
    public BootstrapNavbarButtonState getState() {
        return (BootstrapNavbarButtonState) super.getState();
    }

    @Override
    public VBootstrapNavbarButton getWidget() {
        return (VBootstrapNavbarButton) super.getWidget();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapNavbarButtonState state = getState();

        VBootstrapNavbarButton navbarButton = getWidget();

        navbarButton.setText(state.getText());
        navbarButton.setType(ButtonType.valueOf(state.getType()));
        navbarButton.setSize(ButtonSize.valueOf(state.getSize()));
//        navbarButton.setEnabled(state.isButtonEnabled());
        navbarButton.setToggle(state.isToggle());
    }

    public void onClick(ClickEvent event) {
        MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(
                event.getNativeEvent(), getWidget().getElement());

        rpc.click(details);
    }

}